﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BasicCMS.Content.Parts;
using BasicCMS;
using System.Data;

namespace BasicCMS.Content.Controllers
{
    public class ModelHelper
    {
        public static void BuildViewState(ContentType contentType, FormCollection form, Controller controller)
        {
            foreach (var part in contentType.Parts)
            {
                part.Controller = controller;
                part.Build(form);
            }
        }

        public static void BuildViewState(ContentType contentType, DataRow row, Controller controller)
        {
            foreach (var part in contentType.Parts)
            {
                part.Controller = controller;
                part.Build(row);
            }
        }

        public static ContentType CreateContentType()
        {
            ContentType contentType = new ContentType
            {
                TableName="User",
                Parts = new List<ContentPart>
                {
                    new TextBoxPart{Name="Name",UniqueName="Name"},
                    new TextBoxPart{Name="Password", UniqueName="Password"},
                }
            };

            return contentType;
        }

        public static IDictionary<string, object> GetContentValue(ContentType contentType)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            contentType.Parts.ForEach(m =>
            {
                foreach (var kvp in m.GetData())
                {
                    result.Add(kvp.Key, kvp.Value);
                }
            });
            return result;
        }
    }
}